package topin.facecat.FaceCat.Service

import facecat.topin.core.FCBinary
import facecat.topin.service.FCClientService
import facecat.topin.service.FCMessage

class PriceDataServiceClient : FCClientService() {
    //取消推送
    var FUNCTION_UNSUBCODES = 1

    var socketID = 0

    init {
        serviceID = SERVICEID
    }

    /*
     * 注册代码
     *@param requestID 请求ID
     */
    fun subCodes(requestID: Int, codes: String?) {
        val bw = FCBinary()
        try {
            bw.writeString(codes)
            val bytes = bw.bytes
            val ret = send(
                FCMessage(
                    serviceID, FUNCTION_SUBCODES, requestID,
                    socketID, 0, compressType, bytes.size, bytes
                )
            )
            bw.close()
        } catch (ex: Exception) {
        }
    }

    /*
     *反注册代码
     *@param requestID 请求ID
     *@param codes 代码
     */
    fun unSubCodes(requestID: Int) {
        val bw = FCBinary()
        try {
            bw.writeString("1")
            val bytes = bw.bytes
            val ret = send(
                FCMessage(
                    serviceID, FUNCTION_UNSUBCODES, requestID,
                    socketID, 0, compressType, bytes.size, bytes
                )
            )
            bw.close()
        } catch (ex: Exception) {
        }
    }

    /*
     * 接收消息方法
     *@param message 消息
     */
    override fun onReceive(message: FCMessage) {
        super.onReceive(message)
        sendToListener(message)
    }

    companion object {
        var SERVICEID = 10004

        //注册推送
        var FUNCTION_SUBCODES = 0

        //最新数据
        var FUNCTION_NEWDATA = 2

        /*
         * 获取报价数据
         *@param body 包体
         *@param bodyLength 包体长度
         */
        fun getPriceDatas(body: ByteArray?, bodyLength: Int): ArrayList<PriceData> {
            val priceDatas = ArrayList<PriceData>()
            try {
                val br = FCBinary()
                br.write(body, bodyLength)
                if (body != null && body.size > 0) {
                    val datasSize = br.readInt()
                    for (i in 0 until datasSize) {
                        val data = PriceData()
                        data.m_code = br.readString()
                        data.m_lastClose = br.readFloat().toDouble()
                        data.m_close = br.readFloat().toDouble()
                        val type = br.readByte().toInt()
                        if (type == 0) {
                            data.m_volume = br.readDouble()
                            data.m_amount = br.readDouble()
                        } else {
                            data.m_volume = br.readInt().toDouble()
                            data.m_amount = br.readInt().toDouble()
                        }
                        data.m_high = br.readFloat().toDouble()
                        data.m_low = br.readFloat().toDouble()
                        data.m_upperLimit = br.readFloat().toDouble()
                        data.m_lowerLimit = br.readFloat().toDouble()
                        data.m_ratio = br.readFloat().toDouble()
                        data.m_pe = br.readFloat().toDouble()
                        data.m_turnoverRate = br.readFloat().toDouble()
                        data.m_marketValue = br.readFloat().toDouble()
                        data.m_flow = br.readFloat().toDouble()
                        data.m_speed = br.readFloat().toDouble()
                        priceDatas.add(data)
                    }
                }
                br.close()
            } catch (ex: Exception) {
            }
            return priceDatas
        }
    }
}